package com.ulook.lookplanet.ui.matching.heart;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.common.AppServer;
import com.ulook.lookplanet.data.model.TPlanet;
import com.ulook.lookplanet.util.net.AsynRequestUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.RequestParams;
import com.ulook.lookplanet.util.net.ResponseCallback;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/5/11
 */
public class HeartViewModel extends BaseViewModel2 {

    private int currUserId;
    private MutableLiveData<TPlanet> planetInfo = new MutableLiveData<>();

    public static final int RES_LEN = 4;
    public static final int ASK_SIZE = 6;
    private Ask[] asks;
    // 外向、理性、判断、抽象
    private int[] res = new int[RES_LEN];
    private static final int WX = 1;
    private static final int LX = 2;
    private static final int PD = 3;
    private static final int CX = 4;
    private static final int DMT = 5;

    public int getCurrUserId() {
        return currUserId;
    }

    public void setCurrUserId(int currUserId) {
        this.currUserId = currUserId;
        initAsk();
    }

    public LiveData<TPlanet> getPlanetInfo(){
        return planetInfo;
    }

    /**
     * 初始化问题
     */
    private void initAsk(){
        asks = new Ask[ASK_SIZE];
        asks[0] = new Ask("你更喜欢被认为是一个","坚决的，不轻易心软的人","心肠软的人",
                new int[]{0,1,2,0}, new int[]{0,2,1,0});
        asks[1] = new Ask("在熟悉朋友眼中你是一个怎样的人","善于帮助他们了解自己的情绪","会帮朋友做符合逻辑的决定",
                new int[]{2,0,1,0}, new int[]{1,0,2,0});
        asks[2] = new Ask("在说话时，你更看重","达成结果，有时过于直接让别人不高兴","人际感受，有时可能不愿讲真话",
                new int[]{2,0,0,1}, new int[]{1,0,0,2});
        asks[3] = new Ask("在工作上，你表现出更多的是","充满热枕，有很多想法","心思细腻，为人可靠",
                new int[]{2,1,0,0}, new int[]{1,2,0,0});
        asks[4] = new Ask("日常生活中，对一些要做的小事，你通常","能清楚的记得","常会忘记，但重要的事从不含糊",
                new int[]{0,2,0,1}, new int[]{0,1,0,2});
        asks[5] = new Ask("选择你比较喜欢的","捕捉生命中的精彩点滴","探寻生命的意义和真谛",
                new int[]{0,0,2,1}, new int[]{0,0,1,2});
    }

    /**
     * 获取问题
     */
    public Ask getAsks(int i) {
        return asks[i];
    }

    /**
     * 加上选择结果的值
     */
    public void addOpt(int[] opt){
        for(int i = 0; i < RES_LEN; i++){
            res[i] += opt[i];
        }
    }

    /**
     * 计算结果
     */
    public void calculate(){
        int wx = res[0];
        int lx = res[1];
        int pd = res[2];
        int cx = res[3];

        if(wx > lx && wx >pd && wx >cx){
            updatePlanet(WX);
        }else if (lx > wx && lx > pd && lx > cx) {
            updatePlanet(LX);
        }else if (pd > wx && pd > lx && pd > cx){
            updatePlanet(PD);
        }else if (cx > wx && cx > lx && cx > pd){
            updatePlanet(CX);
        }else {
            updatePlanet(DMT);
        }
    }

    /**
     * 设置星球
     */
    private void updatePlanet(int uid){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                TPlanet planet = (TPlanet) res;
                planetInfo.setValue(planet);
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("服务器异常");
            }
        };

        RequestParams params = new RequestParams();
        params.put("userId", currUserId);
        params.put("planetId", uid);

        AsynRequestUtil.post(AppServer.ROOT_PATH + "/matching/updatePlanet", params, TPlanet.class, callback);

    }
}
